package com.hdax.service.Impl;

import com.hdax.entity.Department;
import com.hdax.entity.Role;
import com.hdax.mappers.DepartmentMapper;
import com.hdax.mappers.EmployeeInfoMapper;
import com.hdax.mappers.EmployeeMapper;
import com.hdax.service.DepartmentService;
import com.hdax.service.RoleService;
import com.hdax.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 部门服务impl
 *
 * @author Aizen
 * @date 2022/05/04
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeeInfoMapper employeeInfoMapper;
    @Autowired
    private RoleService roleService;


    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    public Department findById(int id) {
        return departmentMapper.findById(id);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class,propagation = Propagation.REQUIRED)
    public ResultUtil addDepartment(Department department) {
        try {
            //查询是否有重名的部门
            List<Department> departmentList = departmentMapper.findAll();
            for (Department d : departmentList) {
                if (d.getName().equals(department.getName())) {
                    return ResultUtil.updataResult2("部门名称已存在！",-1);
                }
            }
            //添加部门
            departmentMapper.addDepartment(department);
            //添加新部门的经理角色
            Role role = new Role();
            role.setDescription(department.getName()+"经理");
            role.setDepartmentId(department.getId());
            roleService.addRole(role);
            //添加新部门的普通角色
            Role role1 = new Role();
            role1.setDescription(department.getName()+"员工");
            role1.setDepartmentId(department.getId());
            roleService.addRole(role1);
            //更新角色编号
            if(department.getDperson()!=1){
                employeeMapper.updateRoleIdByEmpId(role.getId(),department.getDperson());
                employeeInfoMapper.updateRoleIdById(role.getId(),department.getDperson());
            }
            //执行员工 所属部门更新
            Integer empId = department.getDperson();
            Integer departmentId = department.getId();
            if(empId!=1){
                employeeMapper.updateDepartmentIdByEmpId(departmentId,empId);
                employeeInfoMapper.updateDepartmentIdById(departmentId,empId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.updataResult2("更新成功！",1);
    }

    @Override
    public List<Department> listAll(int dperson) {
        return departmentMapper.listAll(dperson);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class,propagation = Propagation.REQUIRED)
    public ResultUtil updateDepartment(Department department) {
        //查询到原来的经理 看是不是跟提交的一至
        Department dep = departmentMapper.findById(department.getId());
        if(!dep.getDperson().equals(department.getDperson())){
            //查询当前部门下的经理
            Role role = roleService.findIdByDepartmentId(department.getId());
            //查询当前部门下非经理角色
            Role role1 = roleService.findIdByDepartmentId2(department.getId());
            //执行员工的 角色更新
            if(department.getDperson()!=1){
                employeeMapper.updateRoleIdByEmpId(role.getId(),department.getDperson());
                employeeInfoMapper.updateRoleIdById(role.getId(),department.getDperson());
            }
            employeeMapper.updateRoleIdByEmpId(role1.getId(),dep.getDperson());
            employeeInfoMapper.updateRoleIdById(role1.getId(),dep.getDperson());
            //执行员工 所属部门更新
            Integer empId = department.getDperson();
            Integer departmentId = department.getId();
            if(empId!=1){
                employeeMapper.updateDepartmentIdByEmpId(departmentId,empId);
                employeeInfoMapper.updateDepartmentIdById(departmentId,empId);
            }
        }else{
            department.setDperson(null);
        }
        if(departmentMapper.updateDepartment(department)>0){
            return ResultUtil.updataResult2("更新成功！",1);
        }
        return ResultUtil.updataResult2("更新失败",-1);
    }

    @Override
    public ResultUtil deleteDepartment(Integer depId) {
        //获取部门下的员工数量
        Integer empCount = employeeInfoMapper.getEmployeeInfoCountByDepartmentId(depId);
        if(empCount == 0){
            //执行删除
            departmentMapper.deleteDepartment(depId);
            roleService.deleteroleByDepartmentId(depId);
            return ResultUtil.updataResult2("删除成功！",1);
        }else if (empCount == 1){
            //查查是不是admin
            Department department = departmentMapper.findById(depId);
            if(department.getDperson() == 1) {
                //执行删除
                departmentMapper.deleteDepartment(depId);
                roleService.deleteroleByDepartmentId(depId);
                return ResultUtil.updataResult2("删除成功！", 1);
            }
        }
        return ResultUtil.updataResult2("该部门下还有员工，不能删除哦",-1);
    }
}
